package com.shitou.web;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.SessionAware;

import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;
import com.shitou.pojo.Message;
import com.shitou.pojo.User;
import com.shitou.service.UserService;

@Results({@Result(name=BaseAction.INDEX,location="index.jspx",type="redirectAction"),@Result(name=BaseAction.RELOAD,location="user.jspx",type="redirectAction")})
public class UserAction extends BaseAction implements SessionAware{

	private static final long serialVersionUID = 1L;
	public static final String LOGIN = "login";
	private User user;
	private int id;
	private String name;
	private String password;
	private String validatecode;
	@Resource
	private UserService userService;

	@Override
	public String execute() throws Exception {
		if(getSessionAttribute("user") == null) {
			return LOGIN;
		} else {
			user = userService.findById(id);
			return SUCCESS;
		}
	}

	@SuppressWarnings("unchecked")
	public String login() throws Exception {
		try {
			
			user = new User();
			user.setName(name);
			user.setPassword(password);
			Map<String, Integer> map = (Map<String, Integer>)getSessionAttribute("loginmap");
			if(map == null) {
				map = new HashMap<String, Integer>();
			}
			if(map.get(name) == null || map.get(name) <= 3) {
				SecurityUtils.getSubject().login(new UsernamePasswordToken(name,password));
				setSessionAttribute("user", userService.login(user));
				Message msg = new Message();
				msg.setMessage("登陆成功,正在跳转...");
				msg.setType(Message.SUCCESS);
				JSONArray json = JSONArray.fromObject(msg);
				renderJson(json.toString());
			} else if(map.get(name) >= 3 && map.get(name) <=6) {
				SecurityUtils.getSubject().login(new UsernamePasswordToken(name,password));
				Boolean isTrue = SimpleImageCaptchaServlet.validateResponse(request, validatecode);
				if(isTrue) {
					setSessionAttribute("user", userService.login(user));
					Message msg = new Message();
					msg.setMessage("登陆成功");
					msg.setType(Message.SUCCESS);
					JSONArray json = JSONArray.fromObject(msg);
					renderJson(json.toString());
				} else {
					Message msg = new Message();
					msg.setMessage("验证码输入错误");
					msg.setType(Message.ERROR);
					Integer count = map.get(name);
					if(count == null) {
						map.put(name, 1);
					} else {
						map.put(name, count+1);
					}
					JSONArray json = JSONArray.fromObject(msg);
					renderJson(json.toString());
				}
			} else {
				Message msg = new Message();
				msg.setMessage("登陆失败超过6次，该账户已被禁用");
				msg.setType(Message.FERROR);
				JSONArray json = JSONArray.fromObject(msg);
				renderJson(json.toString());
				
			}
			return null;
		} catch (AuthenticationException e) {
			Map<String, Integer> map = (Map<String, Integer>) getSessionAttribute("loginmap");
			if(map == null) {
				map = new HashMap<String, Integer>();
			}
			Integer count = map.get(name);
			if(count == null) {
				map.put(name, 1);
			} else {
				map.put(name, count+1);
			}
			setSessionAttribute("loginmap", map);
			Message msg = new Message();
			if(map.get(name) >= 3 && map.get(name) <=6) {
				msg.setMessage("用户名或密码错误！");
				msg.setType(Message.TERROR);
				JSONArray json = JSONArray.fromObject(msg);
				renderJson(json.toString());
			} else if(map.get(name) > 6) {
				msg.setMessage("登陆失败超过6次，该账户已被禁用");
				msg.setType(Message.FERROR);
				JSONArray json = JSONArray.fromObject(msg);
				renderJson(json.toString());
			} else {
				msg.setMessage("用户名或密码错误");
				msg.setType(Message.ERROR);
				JSONArray json = JSONArray.fromObject(msg);
				renderJson(json.toString());
			}
			return null;
		}
	}
	
	@Override
	public String del() throws Exception {
		return null;
	}

	@Override
	public String input() throws Exception {
		return LOGIN;
	}

	@Override
	public String list() throws Exception {
		return null;
	}

	@Override
	public String save() throws Exception {
		return null;
	}

	public String quit() throws Exception {
		removeSessionAttribute("user");
		SecurityUtils.getSubject().logout();
		return RELOAD;
	}

	
	//getter and setter
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getValidatecode() {
		return validatecode;
	}

	public void setValidatecode(String validatecode) {
		this.validatecode = validatecode;
	}
	

}
